package com.pcg.posgasstation.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.pcg.posgasstation.R;
import com.pcg.posgasstation.adapter.OrderHistoryAdapter;
import com.pcg.posgasstation.base.BaseActivity;
import com.pcg.posgasstation.base.BaseFragment;
import com.pcg.posgasstation.utils.KeyboardUtil;
import com.pcg.posgasstation.view.fragments.AllOrderFragment;
import com.pcg.posgasstation.view.fragments.SearchOrderFragment;
import com.pcg.posgasstation.view.fragments.UnSignOrderFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class OrderHistoryActivity extends BaseActivity {

    @BindView(R.id.layTvBack)
    ImageView mLayTvBack;
    @BindView(R.id.layBack)
    RelativeLayout mLayBack;
    @BindView(R.id.layTvTitle)
    TextView mLayTvTitle;
    @BindView(R.id.tvAllOrdersTab)
    TextView mTvAllOrdersTab;
    @BindView(R.id.tvUnSignOrdersTab)
    TextView mTvUnSignOrdersTab;
    @BindView(R.id.edtSearchOrder)
    EditText mEdtSearchOrder;
    @BindView(R.id.flContainer)
    FrameLayout mFlContainer;

    private BaseFragment mCurrentFragment;

    private AllOrderFragment mAllOrderFragment;
    private UnSignOrderFragment mUnSignOrderFragment;
    private SearchOrderFragment mSearchOrderFragment;

    private int mCurrentTab;
    private static final int TAB_ALL = 11;
    private static final int TAB_UNSIGN = 12;

    public static void startActivity(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, OrderHistoryActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_history);
        ButterKnife.bind(this);

        initFragments();
        initViews();
    }

    @OnClick(R.id.layBack)
    void backClick() {
        finish();
    }

    @OnClick(R.id.tvAllOrdersTab)
    void allOrdersTabClick() {
        setCurrentTabSelected(TAB_ALL);
    }

    @OnClick(R.id.tvUnSignOrdersTab)
    void unSignOrdersTabClick() {
        setCurrentTabSelected(TAB_UNSIGN);
    }

    private void initFragments() {
        mAllOrderFragment = AllOrderFragment.newInstance();
        mUnSignOrderFragment = UnSignOrderFragment.newInstance();
        mSearchOrderFragment = SearchOrderFragment.newInstance();
    }

    private void initViews() {
        mLayTvTitle.setText("订单列表");

        mEdtSearchOrder.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(mEdtSearchOrder.getText().toString())) {
                    handleSearch(mEdtSearchOrder.getText().toString());
                }
            }
        });

        mEdtSearchOrder.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    handleSearch(mEdtSearchOrder.getText().toString());
//                    KeyboardUtil.hideSoftInput(OrderHistoryActivity.this);
                    return true;
                }
                return false;
            }
        });

        setCurrentTabSelected(TAB_ALL);
    }

    private void handleSearch(String searchText) {
        if (TextUtils.isEmpty(searchText)) {
            setCurrentTabSelected(mCurrentTab);
        } else {
            mSearchOrderFragment.resetPage();
            setCurrentFragment(mSearchOrderFragment);
            mSearchOrderFragment.searchOrderByKeywords(searchText);
        }
    }

    private void setCurrentTabSelected(int currentTab) {
        mCurrentTab = currentTab;
        if (currentTab == TAB_ALL) {
            mTvAllOrdersTab.setBackgroundResource(R.color.btn_bg_color);
            mTvAllOrdersTab.setTextColor(ContextCompat.getColor(mContext, R.color.white));
            mTvUnSignOrdersTab.setBackgroundResource(R.color.white);
            mTvUnSignOrdersTab.setTextColor(ContextCompat.getColor(mContext, R.color.black));
            setCurrentFragment(mAllOrderFragment);
        } else if (currentTab == TAB_UNSIGN) {
            mTvAllOrdersTab.setBackgroundResource(R.color.white);
            mTvAllOrdersTab.setTextColor(ContextCompat.getColor(mContext, R.color.black));
            mTvUnSignOrdersTab.setBackgroundResource(R.color.btn_bg_color);
            mTvUnSignOrdersTab.setTextColor(ContextCompat.getColor(mContext, R.color.white));
            setCurrentFragment(mUnSignOrderFragment);
        }
    }

    private void setCurrentFragment(BaseFragment fragment) {
        if (mCurrentFragment != null && mCurrentFragment == fragment) {
            return;
        }
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (!fragment.isAdded()) {
            transaction.add(R.id.flContainer, fragment);
        } else {
            transaction.show(fragment);
        }
        if (!(fragment instanceof AllOrderFragment) && mAllOrderFragment.isAdded()) {
            transaction.hide(mAllOrderFragment);
        }
        if (!(fragment instanceof UnSignOrderFragment) && mUnSignOrderFragment.isAdded()) {
            transaction.hide(mUnSignOrderFragment);
        }
        if (!(fragment instanceof SearchOrderFragment) && mSearchOrderFragment.isAdded()) {
            transaction.hide(mSearchOrderFragment);
        }
        transaction.commit();
        mCurrentFragment = fragment;
    }

}
